﻿using Microsoft.AspNetCore.Mvc;
using UpperComputer.Application;
using YFurion.Application;
using YFurion.Controllers;

namespace UpperComputer.Web.Entry.Controllers
{
    /// <summary>
    /// 角色控制器
    /// </summary>
    [ApiDescriptionSettings(ApiGroupConst.USER_AUTHORIZATION)]
    public class RoleController : AuthorizeController
    {
        private readonly IBaseRoleService _roleService;
        private readonly IBaseRolePermissionService _rolePermissionService;
        private readonly IBaseMenuService _menuService;
        private readonly IBaseUserService _baseUserService;

        /// <summary>
        /// 角色控制器构造函数
        /// </summary>
        /// <param name="roleService">角色服务</param>
        /// <param name="rolePermissionService">角色权限服务</param>
        /// <param name="menuService">菜单服务</param>
        /// <param name="baseUserService">用户服务</param>
        public RoleController(
            IBaseRoleService roleService,
            IBaseRolePermissionService rolePermissionService,
            IBaseMenuService menuService,
            IBaseUserService baseUserService
            )
        {
            _roleService = roleService;
            _rolePermissionService = rolePermissionService;
            _menuService = menuService;
            _baseUserService = baseUserService;
        }

        /// <summary>
        /// 获取角色集合
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<BaseRoleResultDto>> Get([FromQuery] BaseRoleResultRequestDto input)
        {
            PagedResultDto<BaseRoleResultDto> baseRoleResultDto = await _roleService.GetAll(input);
            return baseRoleResultDto;
        }

        /// <summary>
        /// 根据id获取角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<BaseRoleResultDto> Get(long id)
        {
            BaseRoleResultDto baseRoleResultDto = await _roleService.Get(id);
            return baseRoleResultDto;
        }

        /// <summary>
        /// 新增角色
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<BaseRoleResultDto> Post(BaseRoleDto input)
        {
            BaseRoleResultDto baseRoleResultDto = await _roleService.Post(input);
            return baseRoleResultDto;
        }

        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<BaseRoleResultDto> Put(BaseRoleDto input)
        {
            BaseRoleResultDto baseRoleResultDto = await _roleService.Put(input);
            return baseRoleResultDto;
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<EntityDto> Delete(long id)
        {
            await _baseUserService.GetUserCount(id);
            await _roleService.Delete(id );
            return new EntityDto { Id = id };
        }

        /// <summary>
        /// 获取权限列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<MenuRoleOutputDto> GetPermissions([FromQuery]BaseRolePermissionResultRequestDto input)
        {
            BaseMenuResultRequestDto menuInput = new BaseMenuResultRequestDto()
            {
                IsActive = true,
                ShowMenuButtons = true,
                CanSearchMenuButtons = true,
            };
            List<BaseMenuResultDto> menus = await _menuService.GetMenus(menuInput);
            PagedResultDto<BaseRolePermissionDto> brpDto = await _rolePermissionService.GetAll(input);

            MenuRoleOutputDto model = new MenuRoleOutputDto
            {
                MenuResultDtos = menus,
                RolePermissionDtos = brpDto.Items
            };
            return model;
        }

        /// <summary>
        /// 分配权限
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [YUnitOfWork]
        public async Task<List<BaseRolePermissionDto>> GivePermissions([FromBody]PermissionInputDto input)
        {
            return await _rolePermissionService.PermissionAsync(input.RoleCode, input.Menus);
        }
    }
}
